const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);

app.use(express.static('../../public_html/blockland/'));
app.use(express.static('../../public_html/libs'));
app.use(express.static('../../public_html/blockland/v3'));
app.get('/',function(req, res) {
    res.sendFile(__dirname + '../../public_html/blockland/v3/index.html');
});
var door;
io.sockets.on('connection', function(socket){
	socket.userData = {room:'', x:0, y:0, z:0, heading:0 };//Default values;
 
	console.log(`${socket.id} connected`);
	socket.emit('setId', { id:socket.id });
	
    socket.on('disconnect', function(){
		socket.broadcast.emit('deletePlayer', { id: socket.id, room:socket.userData.room });
    });	
	
	socket.on('init', function(data){
		console.log(`socket.init ${data.model}`);
		socket.userData.model = data.model;
		socket.userData.colour = data.colour;
		socket.userData.room = data.room;
		socket.room = data.room;
		socket.userData.x = data.x;
		socket.userData.y = data.y;
		socket.userData.z = data.z;
		socket.userData.heading = data.h;
		socket.userData.pb = data.pb,
		socket.userData.action = "Idle";
		socket.join(data.room);
	});
	
	socket.on('update', function(data){
		socket.userData.x = data.x;
		socket.userData.y = data.y;
		socket.userData.z = data.z;
		socket.userData.heading = data.h;
		socket.userData.pb = data.pb,
		socket.userData.action = data.action;
		
	});

	socket.on('door', function (data) {
		io.emit('door', data.door);
	});

	socket.on('chat message', function(data){
		console.log(`chat message:${data.id} ${data.message}`);
		io.to(data.id).emit('chat message', { id: socket.id, message: data.message });
	});

	socket.on('chat message in hall', function (msg) {
		io.emit('chat message in hall', msg);
	});

	socket.on('chat message in game', function (msg) {
		io.emit('chat message in game', msg);
	});

	socket.on('end', function (msg) {
		io.emit('end', msg);
	});

	socket.on('req_remoteData', function (data) {
		const nsp = io.of('/');
		let pack = [];

		for (let id in io.sockets.sockets) {
			const socket = nsp.connected[id];
			//Only push sockets that have been initialised
			if (socket.userData.model !== undefined && socket.userData.room !== '' && socket.userData.room == data.room) {
				pack.push({
					id: socket.id,
					model: socket.userData.model,
					colour: socket.userData.colour,
					room: socket.userData.room,
					x: socket.userData.x,
					y: socket.userData.y,
					z: socket.userData.z,
					heading: socket.userData.heading,
					pb: socket.userData.pb,
					action: socket.userData.action
				});
			}
		}
		if (pack.length > 0) io.in(data.room).emit('remoteData', pack);
	});
});

http.listen(2002, function(){
  console.log('listening on *:2002');
});

// setInterval(function(){
// 	const nsp = io.of('/');
//     let pack = [];
	
//     for(let id in io.sockets.sockets){
//         const socket = nsp.connected[id];
// 		//Only push sockets that have been initialised
// 		if (socket.userData.model!==undefined){
// 			pack.push({
// 				id: socket.id,
// 				model: socket.userData.model,
// 				colour: socket.userData.colour,
// 				room: socket.userData.room,
// 				x: socket.userData.x,
// 				y: socket.userData.y,
// 				z: socket.userData.z,
// 				heading: socket.userData.heading,
// 				pb: socket.userData.pb,
// 				action: socket.userData.action
// 			});    
// 		}
//     }
// 	if (pack.length>0) io.emit('remoteData', pack);
// }, 40);